package cn.com.zero.api.auth.config.properties;

import cn.hutool.core.util.StrUtil;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.SignatureException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;

import javax.annotation.PostConstruct;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * @author xiongxj
 * @version 1.0.0
 * @Description token生成的配置类
 * @createTime 2023/3/16 16:49
 */
@Configuration
@Validated
@ConditionalOnProperty(prefix = TokenProperties.TOKEN_PROPERTIES_PREFIX, name = TokenProperties.TOKEN_PROPERTIES_ENCODE_ENCODE_KEY)
@ConfigurationProperties(prefix = TokenProperties.TOKEN_PROPERTIES_PREFIX, ignoreUnknownFields = false)
public class TokenProperties {

    final static String TOKEN_PROPERTIES_PREFIX = "zero.token";
    final static String TOKEN_PROPERTIES_ENCODE_ENCODE_KEY = "encode-key";
    private final static String DEFAULT_ISSUER = "zero_jwt_token";
    private final static String DEFAULT_SIGNATURE_ALGORITHM = "HS256";

    /**
     * 签发人
     */
    private String issuer;

    /**
     * 加密算法
     */
    private String signatureAlgorithm;

    /**
     * 加密密钥
     */
    @NotEmpty
    private String encodeKey;

    /**
     * 解密密钥
     */
    @NotEmpty
    private String decodeKey;

    /**
     * token过期时间，单位秒
     */
    @NotNull
    private Long expireTime;

    /**
     * 刷新间隔时间，单位秒
     */
    private Long refreshInterval;

    /**
     * 刷新次数
     */
    private Integer refreshTimes;

    /**
     * 检验参数配置是否合理
     */
    @PostConstruct
    public void after() {
        if (refreshTimes != null && refreshTimes > 0) {
            Assert.notNull(refreshInterval, "刷新间隔时间[refresh-interval]不允许为空");
            if (refreshInterval <= 0) {
                throw new IllegalArgumentException("刷新间隔时间[refresh-interval]必须大于0");
            }
            if (expireTime <= refreshInterval) {
                throw new IllegalArgumentException("token过期时间[expire-time]必须大于刷新时间间隔[refresh-interval]");
            }
        }
        if (StrUtil.isEmpty(issuer)) {
            issuer = DEFAULT_ISSUER;
        }
        if (StrUtil.isEmpty(signatureAlgorithm)) {
            signatureAlgorithm = DEFAULT_SIGNATURE_ALGORITHM;
        }
        // 验证签名算法是否符合规范
        try {
            SignatureAlgorithm.forName(this.signatureAlgorithm);
        } catch (SignatureException e) {
            throw new IllegalArgumentException("不支持的加密算法[signature-algorithm]，可支持的加密算法可查看 io.jsonwebtoken.SignatureAlgorithm");
        }
        // 验证密钥是否配置

    }

    public String getIssuer() {
        return issuer;
    }

    public void setIssuer(String issuer) {
        this.issuer = issuer;
    }

    public String getSignatureAlgorithm() {
        return signatureAlgorithm;
    }

    public void setSignatureAlgorithm(String signatureAlgorithm) {
        this.signatureAlgorithm = signatureAlgorithm;
    }

    public String getEncodeKey() {
        return encodeKey;
    }

    public void setEncodeKey(String encodeKey) {
        this.encodeKey = encodeKey;
    }

    public String getDecodeKey() {
        return decodeKey;
    }

    public void setDecodeKey(String decodeKey) {
        this.decodeKey = decodeKey;
    }

    public Long getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Long expireTime) {
        this.expireTime = expireTime;
    }

    public Long getRefreshInterval() {
        return refreshInterval;
    }

    public void setRefreshInterval(Long refreshInterval) {
        this.refreshInterval = refreshInterval;
    }

    public Integer getRefreshTimes() {
        return refreshTimes;
    }

    public void setRefreshTimes(Integer refreshTimes) {
        this.refreshTimes = refreshTimes;
    }
}
